package com.eDriveV2;

import java.io.File;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class FileOverview extends ListActivity {
	/** Called when the activity is first created. */
	private static String FileName;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		File f = new File(Environment.getExternalStorageDirectory(), "eDriveData");
		if (!f.exists()) {
			f.mkdir();
		}
		f = new File(f.getAbsolutePath(), "rides");
		if (!f.exists()) {
			f.mkdir();
		}
		String[] fileNames = f.list();
		if (fileNames.length != 0) {
			setListAdapter(new ArrayAdapter<String>(this, R.layout.fileoverview, fileNames));
	
			final ListView lv = getListView();
			lv.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> a, View v, int position, long id) {
					//start stat choice activity
					FileName = (String)a.getItemAtPosition(position);
					Intent statOverview = new Intent(FileOverview.this, StatOverview.class);
					statOverview.putExtra("fileName", FileName);
					//10 is an random number. Must be used for more advanced stuff. We're not using it :)
					startActivityForResult(statOverview, 10);
				}
			});
		} else {
			Toast.makeText(getApplicationContext(), "No files to find.", 5).show();
			finish();
		}
	}
	public static String getFileName(){
		return FileName;
	}
}